package com.anxin.registerManagement.controller;

import com.anxin.registerManagement.bean.dto.LoginDto;
import com.anxin.registerManagement.bean.dto.UpdatePassWordDto;
import com.anxin.registerManagement.bean.jo.UserSearchJo;
import com.anxin.registerManagement.bean.persist.User;
import com.anxin.registerManagement.log.Log;
import com.anxin.registerManagement.service.UserService;
import com.anxin.registerManagement.util.Assert;
import com.anxin.registerManagement.util.RedisUtil;
import com.anxin.registerManagement.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "用户接口")
@RestController
@RequestMapping("/user")
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @Autowired
    RedisUtil redisUtil;

    @ApiOperation("登录接口")
    @PostMapping("/login")
    public ResultUtil login(@Valid @RequestBody LoginDto loginDto) {
        logger.info("登录数据：{}", loginDto);
        // 获取当前用户
        Subject subject = SecurityUtils.getSubject();
        // 封装账号密码
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(loginDto.getUserName(), loginDto.getPassWord());
        // 执行登录，如果没有异常就说明成功，有异常在全局异常进行捕获
        subject.login(usernamePasswordToken);
        // TODO:这里的jwt需要自己组装，先写死，测试
//        data.put("token", tokenStr);
//        data.put("name", sysUser.getNickname());
//        data.put("avatar", sysUser.getAvatar());

        Map<String, Object> data = new HashMap<>();
        data.put("token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwYXNzV29yZCI6IjEyMzQ1NiIsInVzZXJOYW1lIjoi5Y-25YmN5ZGIIiwiZXhwIjoxNjQzMDAzOTc0fQ.IGnU_zPD4v7iouMIfvVW99lBqqSh3Ea6IHS0gM_4T5o");
        data.put("name", "叶前呈");
        return ResultUtil.success(data);
    }

    @ApiOperation("注销接口")
    @PostMapping("/logout")
    public ResultUtil logout() {
        return ResultUtil.success();
    }

    @ApiOperation("查询用户")
    @PostMapping("/search")
    public ResultUtil getUserList(@RequestBody UserSearchJo userSearchJo) {
        logger.info("查询用户：{}", userSearchJo);
        return ResultUtil.success(userService.getUserList(userSearchJo));
    }

    @ApiOperation("获取用户信息")
    @GetMapping("/info")
    public ResultUtil getUserInfo() {
        logger.info("获取用户信息");
        return ResultUtil.success(userService.getUserInfo());
    }

    @ApiOperation("修改用户信息")
    @PostMapping("/updateInfo")
    public ResultUtil updateUserInfo(@RequestBody User user) {
        logger.info("修改用户信息");
        Assert.notNull(user.getId(), "用户id不能为空");
        userService.updateUserInfo(user);
        return ResultUtil.success();
    }

    @ApiOperation("获取所有用户")
    @GetMapping("/get")
    public ResultUtil getAllUser() {
        return ResultUtil.success(userService.getAllUser());
    }

    @ApiOperation("修改用户状态->是否禁用")
    @PostMapping("/isDisabled")
    public ResultUtil updateIsDisabled(@RequestParam("id") Integer id, @RequestParam("isDisabled") boolean isDisabled) {
        userService.updateIsDisabled(id, isDisabled);
        return ResultUtil.success();
    }

    @ApiOperation("添加用户")
    @PostMapping("/add")
    public ResultUtil addUser(@Valid @RequestBody User user) {
        userService.addUser(user);
        return ResultUtil.success();
    }

    @ApiOperation("获取验证码通过邮箱")
    @GetMapping("/sendVerifyCodeByEmail")
    public ResultUtil sendVerifyCodeByEmail(@RequestParam("userName") String userName, @RequestParam("passWord") String passWord) {
        return ResultUtil.success(userService.sendVerifyCodeByEmail(userName, passWord));
    }

    @ApiOperation("校验账号密码")
    @GetMapping("/verifyUsernamePassword")
    public ResultUtil verifyUsernamePassword(@RequestParam("userName") String userName, @RequestParam("passWord") String passWord) {
        userService.verifyUsernamePassword(userName, passWord);
        return ResultUtil.success();
    }

    @ApiOperation("校验验证码")
    @GetMapping("/verifyCode")
    public ResultUtil verifyCode(@RequestParam("uuid") String uuid, @RequestParam("code") String code) {
        userService.verifyCode(uuid, code);
        return ResultUtil.success();
    }

    @ApiOperation("修改密码")
    @PostMapping("/updatePassword")
    public ResultUtil updatePassword(@RequestBody UpdatePassWordDto updatePassWordDto) {
        userService.updatePassword(updatePassWordDto);
        return ResultUtil.success();
    }

    @ApiOperation("修改是否禁用")
    @PostMapping("/updateIsDisabled")
    public ResultUtil updateIsDisabled(@RequestParam("id") int id, @RequestParam("isDisabled") boolean isDisabled) {
        userService.updateIsDisabled(id, isDisabled);
        return ResultUtil.success();
    }

    @ApiOperation("修改默认禁用模式")
    @PostMapping("/updateIsDisabledMode")
    public ResultUtil updateIsDisabledMode(@RequestParam("id") int id, @RequestParam("isDisabledMode") boolean isDisabledMode) {
        userService.updateIsDisabledMode(id, isDisabledMode);
        return ResultUtil.success();

    }

    @ApiOperation("用户列表功能——修改用户信息")
    @PostMapping("/updateUserInfoByUserList")
    public ResultUtil updateUserInfoByUserList(@RequestBody User user) {
        userService.updateUserInfoByUserList(user);
        return ResultUtil.success();
    }

    @ApiOperation("通过id获取用户信息")
    @GetMapping("/getUserById")
    public ResultUtil getUserById(@RequestParam("id") int id) {
        return ResultUtil.success(userService.getUserById(id));
    }

}
